package com.packt.animation.funky;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.util.Log;




public class FunkyActivity extends Activity {
	/** Called when the activity is first created. */
	private ImageView animImage;
	private AnimationDrawable danceAnim;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		View contentView = (View) findViewById(R.layout.main);
		
		animImage = (ImageView) findViewById(R.id.stickmanView);
		final AnimationDrawable stickmanAnim = (AnimationDrawable) animImage.getDrawable();
		Log.d("stickmanAnim", (new Integer(stickmanAnim.getLevel()).toString()));
		
// Folgenden geht definitiv nicht!		
//		danceAnim = new AnimationDrawable();
//		Log.d("danceAnim", (new Integer(danceAnim.getLevel()).toString()));

// Das ist wohl der Hammer es geht nur mit einer Anim aus der Resource		
		danceAnim = (AnimationDrawable) animImage.getDrawable();
		Log.d("danceAnim", (new Integer(danceAnim.getLevel()).toString()));
		
		
		danceAnim.setOneShot( false );
		AnimationDrawable danceLeftAnim = (AnimationDrawable) getResources().getDrawable(R.anim.dance_left);
		attachAnim( danceAnim, danceLeftAnim, 1, 12 );
		AnimationDrawable danceRightAnim = (AnimationDrawable) getResources().getDrawable(R.anim.dance_right);
		attachAnim( danceAnim, danceRightAnim, 1, 11 );
		AnimationDrawable jumpAnim = (AnimationDrawable) getResources().getDrawable(R.anim.jump);
		attachAnim( danceAnim, jumpAnim, 1, 11 );
		
		animImage.setImageDrawable(danceAnim);
		danceAnim = (AnimationDrawable)animImage.getDrawable();
		danceAnim.setVisible(true,true);	
//		contentView.measure(contentView.getWidth(), contentView.getHeight());
		
		animImage.post( 
			new Runnable() {
				public void run() { 
					danceAnim.start();
				}
			}
		);
		
		Button danceLeftButton = (Button) findViewById(R.id.danceleft);
		danceLeftButton.setOnClickListener(
			new View.OnClickListener() {
				public void onClick(View v) {
					AnimationDrawable danceLeftAnim = (AnimationDrawable) getResources().getDrawable(R.anim.dance_left);
					animImage.setImageDrawable(danceLeftAnim);
					danceLeftAnim.start();
				}
			}
		);

		Button danceRightButton = (Button)findViewById(R.id.danceright);
		danceRightButton.setOnClickListener(
			new View.OnClickListener() {
				public void onClick(View v) {
					AnimationDrawable danceRightAnim = (AnimationDrawable) getResources().getDrawable(R.anim.dance_right);
					animImage.setImageDrawable(danceRightAnim);
					danceRightAnim.start();
				}
			}
		);

		Button jumpButton = (Button) findViewById(R.id.jump);
		jumpButton.setOnClickListener(
			new View.OnClickListener() {
				public void onClick(View v) {
					AnimationDrawable jumpAnim = (AnimationDrawable) getResources().getDrawable(R.anim.jump);
					animImage.setImageDrawable(jumpAnim);
					jumpAnim.start();
				}
			}
		);
		
		Button keepOnDancingButton = (Button) findViewById(R.id.keep);
		 keepOnDancingButton.setOnClickListener(
			new View.OnClickListener() {
				public void onClick(View v) {
					AnimationDrawable animDrawable;
					animDrawable = (AnimationDrawable)animImage.getDrawable();
					animDrawable.stop();
					animImage.setImageDrawable(danceAnim);
					danceAnim.setVisible(true,true);
					danceAnim.start();
				}
			}
		);
		 
		Button restButton = (Button) findViewById(R.id.rest);
		restButton.setOnClickListener(
			new View.OnClickListener() {
				public void onClick(View v) {
					AnimationDrawable animDrawable;
					animDrawable = (AnimationDrawable)animImage.getDrawable();
					animDrawable.stop();
				}
			}
		);
		
		Button runButton = (Button) findViewById(R.id.run);
		runButton.setOnClickListener(
			new View.OnClickListener() {
				public void onClick(View v) {
					AnimationDrawable animDrawable;
					animDrawable = (AnimationDrawable)animImage.getDrawable();
					animDrawable.start();
				}
			}
		);
	}

	private void attachAnim( AnimationDrawable destAnim, AnimationDrawable srcAnim, int start, int end ) {
		for (int i = start; i<=end; ++i) {
			destAnim.addFrame( srcAnim.getFrame (i), srcAnim.getDuration (i) );
		}
	}
}
